package com.rongxun.hbwallet.mvp.presenter;

import android.support.annotation.NonNull;
import android.widget.Toast;

import com.rongxun.base.BasePresenter;
import com.rongxun.hbwallet.mvp.contract.IWalletManager;
import com.rongxun.utils.RxUtil;
import com.rongxun.utils.wallet.ETHWallet;
import com.rongxun.utils.wallet.WalletDaoUtils;
import com.rongxun.utils.wallet.WalletSubscriber;

import java.util.List;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableOnSubscribe;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/8/8 14:52
 * 描述	      钱包管理
 */

public class WalletManagerPresenter extends BasePresenter<IWalletManager.IView, IWalletManager.IModel> implements IWalletManager.IPresenter {

    public WalletManagerPresenter(@NonNull IWalletManager.IView pUi) {
        super(pUi);
    }

    public WalletManagerPresenter(@NonNull IWalletManager.IView pUi, @NonNull IWalletManager.IModel pModel) {
        super(pUi, pModel);
    }

    @Override
    public void loadAllWallets() {
        addSubscribe(Flowable.create((FlowableOnSubscribe<List<ETHWallet>>) e -> {
            e.onNext(WalletDaoUtils.loadAll());
            e.onComplete();
        }, BackpressureStrategy.BUFFER)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new WalletSubscriber<List<ETHWallet>>(getView()) {
                    @Override
                    public void onSuccess(List<ETHWallet> ethWallets) {
                        mView.showWalletList(ethWallets);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {
                        Toast.makeText(getView().getContext(), "加载失败", Toast.LENGTH_SHORT).show();
                    }
                }));
    }
}
